package com.fitns.user.controller.baseConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fitns.user.service.baseConfig.TbVehicleLederService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.conf.Resp;
import com.fitns.user.dto.baseConfig.FacilityDataVO;
import com.fitns.user.dto.baseConfig.TbVehicleLedgerDTO;
import com.fitns.user.dto.baseConfig.TbVehicleLedgerVO;
import com.fitns.user.model.baseConfig.TbFacilityData;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.ResultData;
import com.fitns.user.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.util.CollectionUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("backend")
@Api(description="车辆台账")
public  class TbVehicleLederController {

	
	@Autowired
	private TbVehicleLederService tbVehicleLederService;
	
	@PostMapping("insert/vehicleLeder")
	@ApiOperation("新增车辆台账")
	public Object insertVehicleLeder(@Validated TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		int insert =  tbVehicleLederService.insertVehicleLeder(tbVehicleLedgerDTO);
		Resp resp = null;
		if(insert!=0) {
			resp = Resp.wrap("车辆台账添加成功");
		}else {
			 resp = Resp.errorWrap("车辆台账添加失败");
		}
		return resp;
	}
	//@PostMapping("insert/vehicleLeder")
	/*@ApiOperation("新增车辆台账")
	public ResultData insertVehicleLeder(@Validated TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		int insert =  tbVehicleLederService.insertVehicleLeder(tbVehicleLedgerDTO);
		 return new ResultData(200,"SUCESS",insert,"");*/
		/*Resp resp = null;
		if(insert!=0) {
			resp = Resp.wrap("车辆台账添加成功");
		}else {
			 resp = Resp.errorWrap("车辆台账添加失败");
		}
		return resp;*/
	//}
	 
	@PutMapping("update/vehicleLeder")
	@ApiOperation("修改车辆台账")
	public ResultData updateVehicleLeder(@Validated TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		int update =  tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
		  if(StringUtils.isEmpty(update)){
	            return new ResultData(300,"DEFAULT",update,"");
	        }
	        return new ResultData(200,"SUCESS",update,"");

	        
		/*Resp resp = null;
		if(update!=0) {
			resp = Resp.wrap("车辆台账修改成功");
		}else {
			 resp = Resp.errorWrap("车辆台账修改失败");
		}
		return resp;*/
	}
	/*@PutMapping("update/vehicleLeder")
	@ApiOperation("修改车辆台账")
	public Object updateVehicleLeder(@Validated TbVehicleLedgerDTO tbVehicleLedgerDTO) {
		int update =  tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
		Resp resp = null;
		if(update!=0) {
			resp = Resp.wrap("车辆台账修改成功");
		}else {
			 resp = Resp.errorWrap("车辆台账修改失败");
		}
		return resp;
	}*/
	@DeleteMapping("delete/vehicleLeder")
	@ApiOperation("根据id删除车辆台账")
	public Object deleteVehicleLeder(@RequestParam long id) {
		int delete =  tbVehicleLederService.deleteVehicleLeder(id);
		Resp resp = null;
		if(delete!=0) {
			resp = Resp.wrap("车辆台账删除成功");
		}else {
			 resp = Resp.errorWrap("车辆台账删除失败");
		}
		return resp;
	}
	
	@GetMapping("select/vehicleLeder")
	//@PostMapping("select/vehicleLeder")
	@ApiOperation("查询车辆台账")
    public Object selectVehicleLedger( TbVehicleLedgerDTO tbVehicleLedgerDTO) {
        PageInfo<TbVehicleLedgerVO> bannerId = tbVehicleLederService.selectVehicleLedger(tbVehicleLedgerDTO);
        bannerId.setPageSize(tbVehicleLedgerDTO.getPageSize());
        return Resp.page(bannerId);
    }
	
	@GetMapping("select/vehicleLederbyId")
	@ApiOperation("根据id查看车辆台账具体信息")
	public Object selectVehicleLederById(long id) {
		return tbVehicleLederService.selectVehicleLederById(id);	
	}
	
	 /** 图片上传功能 */ 
		//postman测试以数组进行，用相同的key也就是传多个pic，不同的pic有不同的图片
	//postman测试以数组进行，用相同的key也就是传多个pic，不同的pic有不同的图片
	//postman测试以数组进行，用相同的key也就是传多个pic，不同的pic有不同的图片
	    @ApiOperation("上传图片")
	    @PostMapping("uplode/File")
	    public String insertImage(TbVehicleLedgerDTO tbVehicleLedgerDTO, @RequestParam("pic") MultipartFile[] pic) throws Exception {
	        String images="";
	        // 把图片保存到图片目录下
	        for (int i=0;i<pic.length;i++) {
	            MultipartFile file=pic[i];
	            // 为每个文件生成一个新的文件名
	            String picName = UuidUtils.getUUID();
	            // 取文件扩展名
	            String oriName = file.getOriginalFilename();
	            String extName = oriName.substring(oriName.lastIndexOf("."));
	            // 保存文件
	            //file.transferTo(new File(Constants.IMAGE_PATH + picName + extName));
	            file.transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
	           
	            images+=picName + extName+";";
	        }

	        if(images!=""){
	            images=images.substring(0,images.length()-1);
	        }
	        // 把文件名保存到数据库
	        tbVehicleLedgerDTO.setImage(images);
	        tbVehicleLederService.inserImage(tbVehicleLedgerDTO);
	        return tbVehicleLedgerDTO.getImage();
	    }
	
	/*
	@PostMapping("uplodeFile/vehicle")
	@ApiOperation("上传图片并新增或修改相应信息")
//	public Integer inserIcon(TbVehicleLedgerDTO tbVehicleLedgerDTO,   List<MultipartFile> pic) throws Exception {
	public Integer inserIcon(TbVehicleLedgerDTO tbVehicleLedgerDTO,  HttpServletRequest request) throws Exception {
		MultipartHttpServletRequest mutipart=(MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = mutipart.getFileMap();
		Set<String> keySet = fileMap.keySet();
	for (String key : keySet) {
		MultipartFile item=fileMap.get(key);
        	//if(Validator.isNotNull(item)) {
		//uuid随机生成的图片链接
        	String picName = UuidUtils.getUUID();
        	String oriName = item.getOriginalFilename();
        	//uuid随机生成的图片链接加.图片格式如png，完整生成一个图片
        	String extName = oriName.substring(oriName.lastIndexOf("."));
        	if(Validator.isNotNull(oriName)&&"".equals(oriName)){
				return tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
			}
			// 保存文件
        		item.transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
			// 把文件名保存到数据库
			tbVehicleLedgerDTO.setImage(picName + extName);
			Integer icon = tbVehicleLederService.inserIcon(tbVehicleLedgerDTO);
			return icon;
        	}
        	return tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
       // }
	//return null; 	
	}*/
	/*
	@PostMapping("uplodeFile/vehicle")
	@ApiOperation("上传图片并新增或修改相应信息")
	public Integer inserIcon(TbVehicleLedgerDTO tbVehicleLedgerDTO, MultipartFile pic List<MultipartFile> pic) throws Exception {
		MultipartFile file = null;
		List<MultipartFile> files = new ArrayList<>();
		for (int i = 0; i < files.size(); i++) {
			file = files.get(i);
			if(Validator.isNotNull(file)) {
        	String picName = UuidUtils.getUUID();
        	String oriName = pic.getOriginalFilename();
        	String extName = oriName.substring(oriName.lastIndexOf(".")+1);
        	if(Validator.isNotNull(oriName)&&"".equals(oriName)){
				return tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
			}
        	//String extName = oriName.substring(oriName.lastIndexOf(".")+1);
			// 保存文件
        pic.transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
        	//((MultipartFile) pic).transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
			//((MultipartFile) pic).transferTo(new File(Constants.IMAGE_PATH + picName + extName));
			// 把文件名保存到数据库
			tbVehicleLedgerDTO.setImage(picName + extName);
			Integer icon = tbVehicleLederService.inserIcon(tbVehicleLedgerDTO);
			return icon;
        	}
        	return tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
        }
		return null; 	
	}*/
	
	/*	}
		for (MultipartFile item : pic) {
        	if(Validator.isNotNull(pic)) {
        		String picName = UuidUtils.getUUID();
        		String oriName = item.getOriginalFilename();
        		String extName = oriName.substring(oriName.lastIndexOf(".")+1);
        	String picName = UuidUtils.getUUID();
        	String oriName = item.getOriginalFilename();
        	String extName = oriName.substring(oriName.lastIndexOf(".")+1);
        	if(Validator.isNotNull(oriName)&&"".equals(oriName)){
				return tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
			}
        	//String extName = oriName.substring(oriName.lastIndexOf(".")+1);
			// 保存文件
        pic.transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
        	//((MultipartFile) pic).transferTo(new File("D:\\workspace\\woliu-zhhw\\zhhw\\src\\main\\resources\\image\\"+ picName + extName));
			//((MultipartFile) pic).transferTo(new File(Constants.IMAGE_PATH + picName + extName));
			// 把文件名保存到数据库
			tbVehicleLedgerDTO.setImage(picName + extName);
			Integer icon = tbVehicleLederService.inserIcon(tbVehicleLedgerDTO);
			return icon;
        	}
        	return tbVehicleLederService.updateVehicleLeder(tbVehicleLedgerDTO);
        }
		return null; 	
	
	*/
	
	
	/*@PostMapping("uplodeFile")
		public String handleFileUpload(HttpServletRequest request) {

			List<MultipartFile> files = ((MultipartHttpServletRequest) request)
					.getFiles("file");
			MultipartFile file = null;
	BufferedOutputStream stream = null;

			for (int i = 0; i < files.size(); ++i) {

				file = files.get(i);

				if (!file.isEmpty()) {
					try {
						byte[] bytes = file.getBytes();
						stream = new BufferedOutputStream(new FileOutputStream(
								new File(file.getOriginalFilename())));
						stream.write(bytes);
						stream.close();
					} catch (Exception e) {
						stream = null;
						return "You failed to upload " + i + " => "
								+ e.getMessage();
					}
				} else {
					return "You failed to upload " + i
						+ " because the file was empty.";
				}

			}
			return "upload successful";

		}

	*/
	
	
	
	
	@PostMapping("import/vehicle")
	@ApiOperation("exclue导入车辆台账")
	//工具测试传参  就传一个file就行了
	 public String importData(@RequestParam("file") MultipartFile file) throws Exception {
    	//ExcelUtils.importExcel();   来自于工具类
    	List<List<String>> excelList=ExcelUtils.importExcel(file);
        List<String> headList=excelList.get(0);
        List<TbVehicleLedgerVO> facilityDataVos=new ArrayList<>();
        //boolean is = ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.PLATE_NUM_0.equals(headList.get(0));
        if(ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.PLATE_NUM_0.equals(headList.get(0))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.DRIVER_NAME_1.equals(headList.get(1))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.DRIVER_PHONE_2.equals(headList.get(2))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.TYPE_3.equals(headList.get(3))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.TERMINAL_4.equals(headList.get(4))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.STATUS_5.equals(headList.get(5))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.LET_STANDARD_6.equals(headList.get(6))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.FUEL_TYPE_7.equals(headList.get(7))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.PRODUCT_DATE_8.equals(headList.get(8))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.DURABLE_YEARS_9.equals(headList.get(9))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.RANGES_10.equals(headList.get(10))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.PROPERTY_11.equals(headList.get(11))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.PURPOSE_12.equals(headList.get(12))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.TRANS_TYPE_13.equals(headList.get(13))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.BUY_DATE_14.equals(headList.get(14))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.INSTALL_DATE_15.equals(headList.get(15))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.VALIDE_PERIOD_16.equals(headList.get(16))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.TRANSHIP_17.equals(headList.get(17))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.ROAD_CERTIFICATE_18.equals(headList.get(18))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.BRAND_MODEL_19.equals(headList.get(19))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.CHANGE_COM_20.equals(headList.get(20))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.ENGINE_NUM_21.equals(headList.get(21))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.APPROVED_NUM_22.equals(headList.get(22))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.APPOVED_WEIGHT_23.equals(headList.get(23))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.VEHICLE_POWER_24.equals(headList.get(24))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.FRAME_NUM_25.equals(headList.get(25))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.UNDERPAN_TYPE_26.equals(headList.get(26))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.DEAD_WEIGHT_27.equals(headList.get(27))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.LENGTH_28.equals(headList.get(28))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.WIDTH_29.equals(headList.get(29))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.HEIGHT_30.equals(headList.get(30))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.OIL_WEAR_31.equals(headList.get(31))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.SPEED_LIMIT_32.equals(headList.get(32))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.FETURE_NUM_33.equals(headList.get(33))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.POLE_PAIRS_34.equals(headList.get(34))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.INPUT_VOLTAGE_35.equals(headList.get(35))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.ONE_PARAM_36.equals(headList.get(36))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.TWO_PARAM_37.equals(headList.get(37))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.ILLEGAL_MOVE_38.equals(headList.get(38))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.LINE_ALARM_39.equals(headList.get(39))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.CAMERA_40.equals(headList.get(40))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.VIDEO_41.equals(headList.get(41))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.VIDEO_NUM_42.equals(headList.get(42))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.STOP_AREA_43.equals(headList.get(43))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.STOP_TIME_44.equals(headList.get(44))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.WORK_RATION_45.equals(headList.get(45))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.RECORDER_NUM_46.equals(headList.get(46))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.VIDEO_ADDRESS_47.equals(headList.get(47))&&
		        	ExcelConstants.VEHICLE_LEDGER_IMPORT_HEADER.IMAGE_48.equals(headList.get(48))){
        	 facilityDataVos=tbVehicleLederService.importExcel(excelList);
        }else {
        	TbVehicleLedgerVO facilityDataVo=new TbVehicleLedgerVO();
        	facilityDataVo.setMsg("模板有误，请勿修改模板！");
             facilityDataVos.add(facilityDataVo);
        }

        ResultData resultData=new ResultData();
        String errorData="";
        if (CollectionUtil.isNotEmpty(facilityDataVos)) {
            errorData = JSON.toJSONString(facilityDataVos);
        }
        if(org.apache.commons.lang3.StringUtils.isEmpty(errorData)){
            resultData.setMsg("成功");
        }else{
            resultData.setCode(300);
            resultData.setMsg("失败");
        }

        resultData.setObj(errorData);
        return JSON.toJSONString(resultData);
	}
	
	
	/** 
	*  
	* ################### exclue文件导出，也就是从数据库导出文件到exclue
	* 
	* */
	
	@GetMapping("export/Vehicle")
    @ApiOperation("exclue导出车辆台账")
	 public void exportCustomer(TbVehicleLedgerDTO tbVehicleLedgerDTO, HttpServletResponse response, HttpServletRequest request) throws Exception{
        List<List<String>> excelList =tbVehicleLederService.export(tbVehicleLedgerDTO);
        ExcelUtils.export(response,request,"车辆台账",excelList);
    }
	
	
}
